package cn.cocho.custom.yunCashierDf.news.controller;

import cn.cocho.common.web.app.JsonResultContants;
import cn.cocho.common.web.character.StringUtils;
import cn.cocho.common.web.jqgrid.JQGridContants;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridRules;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.custom.yunCashierDf.news.domain.NewsInfo;
import cn.cocho.custom.yunCashierDf.news.domain.NewsModule;
import cn.cocho.custom.yunCashierDf.news.service.NewsInfoService;
import cn.cocho.custom.yunCashierDf.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by WQJ on 2015/11/12.
 */

@Controller
public class NewsInfoController {

    @Autowired
    private NewsInfoService newsInfoService;

    @ResponseBody
    @RequestMapping(value = "/admin/news/getNewsInfoList")
    public JQGridPage getNewsInfoList(JQGridPage<NewsInfo> pageJQGrid) {
        return newsInfoService.getListByJQgrid(pageJQGrid);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/news/getNewsModuleInfoList")
    public JQGridPage getNewsModuleInfoList(JQGridPage<NewsModule> pageJQGrid) {
        return newsInfoService.getModuleListByJQgrid(pageJQGrid);
    }

    @ResponseBody
    @RequestMapping(value = "/members/news/getAdModuleInfoList")
    public JQGridPage getAdModuleInfoList(JQGridPage<NewsModule> pageJQGrid) {
        pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("category","eq","1"));
        return newsInfoService.getModuleListByJQgrid(pageJQGrid);
    }

    @ResponseBody
    @RequestMapping(value = "/members/news/getDynamicModuleInfoList")
    public JQGridPage getDynamicModuleInfoList(JQGridPage<NewsModule> pageJQGrid) {
        pageJQGrid.getJqGridFilter().getRules().add(new JQGridRules("category","eq","2"));
        return newsInfoService.getModuleListByJQgrid(pageJQGrid);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/news/editNewsInfo")
    public void editNewsInfo(NewsInfo newsInfo, HttpServletResponse response) {
        String warnMessage = null;
        if (newsInfo.getOper().equals(JQGridContants.EDIT_OPER_ADD)) {
            newsInfoService.add(newsInfo);
        } else if (newsInfo.getOper().equals(JQGridContants.EDIT_OPER_UPDATE)) {
            newsInfo.setNewsId(newsInfo.getId());
            newsInfoService.update(newsInfo);
        } else if (newsInfo.getOper().equals(JQGridContants.EDIT_OPER_DEL)) {
            JQGridUtils.delete("delete from news_info where news_id ", newsInfo.getId());
        }
        JQGridUtils.warnUserMessage(warnMessage, response);
    }

    @ResponseBody
    @RequestMapping(value = "/admin/news/getNewsInfoById")
    public JsonResult<NewsInfo> getNewsInfoById(String newsId) {
        JsonResult<NewsInfo> result;
        NewsInfo newsInfo = newsInfoService.getNewsInfoById(newsId);
        if (newsInfo != null) {
            result = new JsonResult<NewsInfo>(JsonResultContants.SUCCESS, "获取公告信息成功！", newsInfo);
        } else {
            result = new JsonResult<NewsInfo>(JsonResultContants.FAIL, "无法公告信息！");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/admin/news/saveNewsInfo", method = RequestMethod.POST)
    public JsonResult saveSalesInfo(NewsInfo newsInfo) {
        JsonResult result;
        if (StringUtils.isEmpty(newsInfo.getNewsId())) { //新增
            boolean insertResult = newsInfoService.insertNewsInfo(newsInfo);
            if (insertResult == true) {
                result = new JsonResult(JsonResultContants.SUCCESS, "新增公告信息成功！", newsInfo.getNewsId());
            } else {
                result = new JsonResult(JsonResultContants.FAIL, "新增公告信息失败！");
            }
        } else {  //修改
            boolean replaceResult = newsInfoService.replaceNewsInfo(newsInfo);
            if (replaceResult) {
                result = new JsonResult(JsonResultContants.SUCCESS, "修改公告信息成功！", newsInfo.getNewsId());
            } else {
                result = new JsonResult(JsonResultContants.FAIL, "修改公告信息失败！");
            }
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/customer/news/getNewsList")
    public JsonResult getNewsList() {
        List<NewsInfo> list = newsInfoService.getNewsInfo();
        JsonResult result = new JsonResult(JsonResultContants.SUCCESS, "获取成功", list);
        return result;
    }
}
